package link.anyauto.j2m;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import link.anyauto.j2m.executor.ExecutorDemo;
import link.anyauto.j2m.executor.ScheduledExecutorDemo;

/**
 * Created by LYQ on 2016/5/12.
 */
public class ExecutorAct extends Activity {

    @Bind(R.id.tv_result)
    TextView tvResult;

    Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_executor);
        ButterKnife.bind(this);
        tvResult.setText("正在执行耗时操作中 ... ... ");
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case MSG_LOG:
                        tvResult.append("\n" + msg.obj);
                        break;
                }
            }
        };
    }

    @OnClick(R.id.btn_plain_pool)
    void plain(View view) {
        new ExecutorDemo(handler).start();
    }

    @OnClick(R.id.btn_scheduled_pool)
    void scheduled(View view) {
        if(sed == null) {
            sed = new ScheduledExecutorDemo(handler);
            sed.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(sed != null) {
            sed.stop();
            sed = null;
        }
    }

    ScheduledExecutorDemo sed;
    public static final int MSG_LOG = 1;
}
